package shop.ynubeggar.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import shop.ynubeggar.request.CreateCategoryRequest;
import shop.ynubeggar.request.UpdateCategoryRequest;
import shop.ynubeggar.service.AdminCategoryService;

import javax.annotation.Resource;

@Api(tags = "商品类别")
@RestController
@RequestMapping("api/admin/category/v1")
public class AdminCategoryController {

    @Resource
    AdminCategoryService adminCategoryService;


    @ApiOperation(value = "分类列表", notes = "获取所有商品分类")
    @GetMapping("/")
    public Object listAllCategory(
            @ApiParam(value = "id") @RequestParam(value = "id", defaultValue = "-1") long id,
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "10") int size) {
        if (id > 0) {
            return adminCategoryService.findOneCategory(id);
        } else {
            return adminCategoryService.listAllCategory(page, size);
        }
    }

    @ApiOperation(value = "分类新增", notes = "暂不支持添加父类")
    @PostMapping("/")
    public void newCategory(@ApiParam(value = "类名") @RequestBody CreateCategoryRequest createCategoryRequest) {
        adminCategoryService.createCategory(createCategoryRequest);
    }

    @ApiOperation(value = "分类调整", notes = "暂不支持添加父类")
    @PutMapping("/{id}")
    public void updateCategory(@ApiParam(value = "分类id")
                               @PathVariable(value = "id") long id,
                               @ApiParam(value = "分类信息")
                               @RequestBody UpdateCategoryRequest updateCategoryRequest) {
        adminCategoryService.updateCategory(id, updateCategoryRequest);
    }

    @ApiOperation(value = "分类删除")
    @DeleteMapping("/{id}")
    public void delOneCategory(@PathVariable(value = "id") long categoryId) {
        adminCategoryService.deleteOneCategory(categoryId);
    }
}
